package com.forotuxpan.appforotuxpan;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by gmartinezh on 20/08/2014.
 */
public class ItemAdapterNoticias extends BaseAdapter {
    private Context context;
    private List<Noticia> items;

    public ItemAdapterNoticias(Context context, List<Noticia> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return this.items.size();
    }

    @Override
    public Object getItem(int position) {
        return this.items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        if (convertView == null) {
            // Create a new view into the list.
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listview_noticias_categoia, parent, false);
        }

        // Set data into the view.
        ImageView ivItem = (ImageView) rowView.findViewById(R.id.ivItem);
        TextView textViewTitulo = (TextView) rowView.findViewById(R.id.txtTitulo);
        TextView textViewDesc = (TextView) rowView.findViewById(R.id.txtDesc);

        Noticia item = this.items.get(position);
        textViewTitulo.setText(item.getTitulo());
        textViewDesc.setText(item.getArticulo());
        if (item.getImage() == null)
            ivItem.setImageResource(R.mipmap.ic_launcher);
        else
            ivItem.setImageBitmap(item.getImage());

        return rowView;
    }
}
